<template>
  <div style="padding: 10px">

    <!--    搜索区域-->
    <div style="margin: 10px 0">
      <el-input v-model="search" placeholder="请输入订单编号" style="width: 20%" clearable></el-input>
      <el-button type="primary" style="margin-left: 5px" @click="load">查询</el-button>
    </div>
    <el-table
        v-loading="loading"
        :data="tableData"
        border
        stripe
        size="medium"
        style="width: 100%"
    >
      <el-table-column
          width="70"
          prop="id"
          label="ID"
          sortable
      >
      </el-table-column>
      <el-table-column
          width="100"
          prop="name"
          label="订单名称">
      </el-table-column>
      <el-table-column
          width="290"
          prop="orderNo"
          label="订单编号">
      </el-table-column>
      <el-table-column
          prop="totalPrice"
          label="积分"
      width="60">
      </el-table-column>
      <el-table-column
          prop="payPrice"
          label="实付款/元"
      width="100">
      </el-table-column>

      <el-table-column
          width="150"
          prop="createTime"
          label="创建时间"
          :formatter="dateFormat">
      </el-table-column>
      <el-table-column
          width="150"
          prop="payTime"
          label="付款时间"
          :formatter="dateFormat">
      </el-table-column>
      <el-table-column
          prop="username"
          label="购买人">
      </el-table-column>
      <el-table-column label="支付状态" width="80">
        <template #default="scope">
          <span v-if="scope.row.status === null" style="color: orange">未支付</span>
          <span v-if="scope.row.status === 1" style="color: green">已支付</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="80">
        <template slot-scope="scope">


          <!--删除-->
          <el-button @click="handleDelete(scope.row.id)" title="删除"
                     slot="reference" type="danger" icon="el-icon-delete" circle
                     style="margin-left: 10px"></el-button>
        </template>
      </el-table-column>
    </el-table>

    <div style="margin: 10px 0">
      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="currentPage"
          :page-sizes="[5, 10, 20]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
      </el-pagination>
    </div>
  </div>
</template>

<script>


import request from "@/utils/request";
export default {
  name: 'Order',
  components: {

  },
  data() {
    return {
      user: {},
      loading: true,
      form: {},
      dialogVisible: false,
      search: '',
      currentPage: 1,
      pageSize: 5,
      total: 0,
      tableData: [],
    }
  },
  created() {
    let userStr = sessionStorage.getItem("user") || "{}"
    this.user = JSON.parse(userStr)
    // 请求服务端，确认当前登录用户的 合法信息


    this.load()
  },
  methods: {
      dateFormat(row, column, cellValue, index) {
          console.log(row, column, cellValue, index)
          const daterc = row[column.property]
          if (daterc != null) {
              var date = new Date(daterc);
              var year = date.getFullYear();
              /* 在日期格式中，月份是从0开始，11结束，因此要加0
               * 使用三元表达式在小于10的前面加0，以达到格式统一  如 09:11:05
               * */
              var month = date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1;
              var day = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
              var hours = date.getHours() < 10 ? "0" + date.getHours() : date.getHours();
              var minutes = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes();
              var seconds = date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();
              // 拼接
              return year + "-" + month + "-" + day + " " + hours + ":" + minutes + ":" + seconds;
          }
      }
      ,
    load() {
      this.loading = true
      request.get("/order", {
        params: {
          pageNum: this.currentPage,
          pageSize: this.pageSize,
          search: this.search
        }
      }).then(res => {
          console.log(res)
        this.loading = false
        this.tableData = res.data.records
        this.total = res.data.total
      })
    },
    add() {
      this.dialogVisible = true
      this.form = {}
      if (this.$refs['upload']) {
        this.$refs['upload'].clearFiles()  // 清除历史文件列表
      }
    },
    save() {
      if (this.form.id) {  // 更新
        request.put("/order", this.form).then(res => {
          console.log(res)
          if (res.code === 20000) {
            this.$toast({
              type: "success",
              message: "更新成功"
            })
          } else {
            this.$toast({
              type: "error",
              message: res.msg
            })
          }
          this.load() // 刷新表格的数据
          this.dialogVisible = false  // 关闭弹窗
        })
      }  else {  // 新增
        request.post("/order", this.form).then(res => {
          console.log(res)
          if (res.code === 20000) {
            this.$toast({
              type: "success",
              message: "新增成功"
            })
          } else {
            this.$toast({
              type: "error",
              message: res.msg
            })
          }

          this.load() // 刷新表格的数据
          this.dialogVisible = false  // 关闭弹窗
        })
      }
    },
    handleEdit(row) {
      this.form = JSON.parse(JSON.stringify(row))
      this.dialogVisible = true
      this.$nextTick(() => {
        if (this.$refs['upload']) {
          this.$refs['upload'].clearFiles()  // 清除历史文件列表
        }
      })

    },
    handleDelete(id) {
      this.$confirm('你确定要删除,是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        request.delete('/order/' + id).then(res => {
            console.log(res.code)
          this.$toast({
            message: '删除成功',
            type: 'success'
          })
          this.load()
        })
      }).catch(() => {
        this.$toast({
          message: '已取消删除',
          type: 'success'
        })
      })
    },
    handleSizeChange(pageSize) {   // 改变当前每页的个数触发
      this.pageSize = pageSize
      this.load()
    },
    handleCurrentChange(pageNum) {  // 改变当前页码触发
      this.currentPage = pageNum
      this.load()
    }
  }
}
</script>
